import {
  Controller,
  Inject,
  Body,
  Post,
  Get,
  Param,
  Put,
  ParseIntPipe,
} from '@nestjs/common';
import { CommunityControlService } from './CommunityControl.service';
import CommunityListDto from '@/dto/CommunityList.dto';
import ExamineCommunityDto from '@/dto/ExamineCommunity.dto';
import {
  CheckAdmin,
  UserInfo,
  RequireLogin,
} from '@/decorator/index.decorator';

@Controller('admin/community')
export class CommunityControlController {
  @Inject(CommunityControlService)
  private readonly communityControlService: CommunityControlService;

  fommatRes(item) {
    item.community_img = item.community_img.map((i: any) => i.img);
    item.username = item.User.username;
    item.avatar = item.User.avatar;

    // eslint-disable-next-line
    const { updatedAt, User, ...rest } = item;
    return rest;
  }

  @Post('list')
  async getCommunityList(@Body() data: CommunityListDto) {
    const { pageNum, pageSize, userId, username, status, sort } = data;
    const res = await this.communityControlService.getCommunityList(
      pageNum,
      pageSize,
      userId,
      username,
      status,
      sort,
    );

    res.list = res.list.map((item: any) => this.fommatRes(item));
    return res;
  }

  @Get('/:id')
  @RequireLogin()
  async getCommunityDetail(
    @UserInfo() userInfo,
    @Param('id', ParseIntPipe) id: number,
  ) {
    const res = await this.communityControlService.getCommunityById(id);

    if (userInfo.phone) {
      const isStar = await this.communityControlService.getStarInfo(
        userInfo.userId,
        id,
      );
      return {
        ...this.fommatRes(res),
        isStar,
      };
    }

    return this.fommatRes(res);
  }

  @Put()
  @CheckAdmin()
  async examineCommunity(@Body() data: ExamineCommunityDto) {
    const { id, status } = data;

    const res = await this.communityControlService.examineCommunity(id, status);
    return '审核' + (res && '成功') || '失败';
  }
}
